//
//  BannerCell.swift
//  edu
//
//  Created by karlzhou on 15/11/5.
//  Copyright © 2015年 karlzhou. All rights reserved.
//


class BannerCell: UICollectionViewCell,UICollectionViewDataSource,UICollectionViewDelegate {
    
    var imageView: UIImageView!
    var tagLb: UILabel!
    var nameLb: UILabel!
    var model : [HomePage] = []
    
    var collectionView: UICollectionView!
    var pageCont : UIPageControl!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCollectionView()
        setupPageContr()
        print(self.subviews)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        print("init with coder")
        fatalError("init(coder:) has not been implemented")
        
    }
    func setupCollectionView(){
        
        let layout =  UICollectionViewFlowLayout()
        layout.itemSize = frame.size
        layout.scrollDirection = .Horizontal
        
        self.collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
        
        self.collectionView.registerClass(HomeCommonCell.self, forCellWithReuseIdentifier: "bannerId")
        
        self.collectionView.pagingEnabled = true
        self.collectionView.bounces = false
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.addSubview(self.collectionView)
    }
    
    
    func setupPageContr(){
        self.pageCont = UIPageControl()
        self.pageCont.numberOfPages = self.model.count
        self.pageCont.currentPageIndicatorTintColor = UIColor.redColor()
        self.pageCont.pageIndicatorTintColor = UIColor.blueColor()
        pageCont.center = CGPoint(x: self.bounds.size.width*0.5, y: self.bounds.size.height-10)
        self.addSubview(self.pageCont)
    }
    
    func setModel(model:[HomePage]) -> Void {
        self.model =  model
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return self.model.count
    }
    
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("bannerId", forIndexPath: indexPath)
        
        let imageView = UIImageView(frame: self.frame)
                imageView.sd_setImageWithURL(NSURL(string: model[indexPath.row].picUrl))
        cell.backgroundView = imageView
        return cell
        
    }
    
    @available(iOS 6.0, *)
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
        return 1
    }
    
}
